package com.example.eeuse.controller;

import com.example.eeuse.model.dto.ChannelStatisticDto;
import com.example.eeuse.model.ee.TaskMonitor;
import com.example.eeuse.model.mp.IotTaskMonitor;
import com.example.eeuse.model.vo.ChannelStatisticVo;
import com.example.eeuse.server.IotTaskMonitorService;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author hly
 */
@RestController
@RequestMapping("/iotTaskMonitor")
public class IotTaskMonitorController {

    @Resource
    private IotTaskMonitorService iotTaskMonitorService;
    @Resource
    private RedissonClient redissonClient;

    /**
     * 查询通道统计信息
     * @param channelStatisticDto 通道统计信息查询条件
     * @return 通道统计信息列表
     */
    @RequestMapping("/channelStatistic")
    public List<ChannelStatisticVo> channelStatistic(@Valid @RequestBody ChannelStatisticDto channelStatisticDto) {
        return iotTaskMonitorService.searchChannelStatistic(channelStatisticDto);
    }

    /**
     * 向队列添加元素
     * @return 是否添加成功
     */
    @RequestMapping("/addElement")
    public Boolean addElement() {
        // 创建一个阻塞队列
        RBlockingQueue<TaskMonitor> blockingQueue = redissonClient.getBlockingQueue("iotChannelMonitorBlockingQueue");
        TaskMonitor iotTaskMonitor = new TaskMonitor();
        iotTaskMonitor.setType("type");
        iotTaskMonitor.setChannelName("channelName");
        iotTaskMonitor.setSuccessNumber(100);
        iotTaskMonitor.setFailNumber(100);
        iotTaskMonitor.setExecuteTime(200);
        iotTaskMonitor.setCreateTime("2021-09-01 12:00:00");
        // 向队列添加元素
        return blockingQueue.offer(iotTaskMonitor);
    }

}
